<!--
* coder  : dzlua
* email  : 505544956@qq.com
* module : ApiMgr-client
* path   : src/views
* file   : register.vue
* time   : 2017-11-15 13:32:54
-->

<template>
  <div class="register">
    <am-header></am-header>
    <el-form class="login-container" v-bind:rules="rules" ref="ruleForm" v-bind:model="ruleForm">
     <el-form-item label="账号" prop="username">
       <el-input type="text" :autofocus="true" auto-complete="off" v-model="ruleForm.username" placeholder="请输入账号"></el-input>
     </el-form-item>
     <el-form-item label="邮箱" prop="email">
       <el-input type="text" auto-complete="off" v-model="ruleForm.email" placeholder="请输入邮箱"></el-input>
     </el-form-item>
     <el-form-item label="密码" prop="password">
       <el-input type="password" auto-complete="off" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
     </el-form-item>
     <el-form-item label="确认密码" prop="password2">
       <el-input type="password" auto-complete="off" v-model="ruleForm.password2" placeholder="请确认密码"></el-input>
     </el-form-item>
     <el-form-item>
       <el-button type="primary" v-on:click="handleSubmit">提交</el-button>
       <el-button v-on:click="handleReset">重置</el-button>
     </el-form-item>
    </el-form>
    <am-footer></am-footer>
  </div>
</template>

<script>
  export default {
    name: 'register',
    data () {
      // 姓名验证
      let validatorName = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入账号'))
        } else if (!/^[A-Za-z0-9_\-\u4e00-\u9fa5]{3,16}$/.test(value)) {
          callback(new Error('账号只能是3-16位中英文数字下划线'))
        } else {
          callback()
        }
      }
      // 邮箱验证
      let validatorEmail = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入邮箱'))
        } else if (!/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value)) {
          callback(new Error('邮箱 格式不正确'))
        } else {
          callback()
        }
      }
      // 密码验证
      let validatorPass = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入密码'))
        } else if (!/^(.){6,16}$/.test(value)) {
          callback(new Error('密码只能是6-16位'))
        } else {
          callback()
        }
      }
      // 重复密码验证
      let validatorPass2 = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入确认密码'))
        } else {
          if (this.ruleForm.password !== value) {
            callback(new Error('两次确认密码不一致'))
          }
          callback()
        }
      }
      return {
        // 表单数据
        ruleForm: {
          username: '',
          email: '',
          password: '',
          password2: ''
        },
        // 验证规则
        rules: {
          username: [
            { validator: validatorName, trigger: 'blur' }
          ],
          email: [
            { validator: validatorEmail, trigger: 'blur' }
          ],
          password: [
            { validator: validatorPass, trigger: 'blur' }
          ],
          password2: [
            { validator: validatorPass2, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 提交函数
      handleSubmit () {
        // 验证表单
        this.$refs.ruleForm.validate(valid => {
          if (!valid) return
          this.$api.req({
            url: 'register_email',
            method: 'POST',
            params: this.$utils.filter(this.ruleForm, [
              'username', 'email', 'password'
            ])
          }, resp => {
            if (!resp.status.success) {
              this.$message.error(resp.status.message)
              return
            }
            this.$message.success(resp.status.message)
            this.$router.push({ name: 'login' })
          })
        })
      },
      // 重置表单函数
      handleReset () {
        this.$refs.ruleForm.resetFields()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .login-container {
    /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
    .title {
      margin: 0px auto 40px auto;
      text-align: center;
      color: #505458;
    }
    .remember {
      margin: 0px 0px 35px 0px;
    }
  }
</style>
